package com.api.sw.duban.util;

import com.alibaba.fastjson.JSONObject;
import com.ibm.icu.text.SimpleDateFormat;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ExportUtil {
    /**
     *
     * @Date 2018年7月19日 上午11:19:04
     * @Description 导出word
     * @Fcunction exportWord
     * @param title
     * @param value_columns
     * @param list
     * @return XWPFDocument
     *
     */
    public static XWPFDocument exportWord(String title, String[] value_columns, List<List<String>> list, int[] colWidths, int one, int two,int W,int H) {
        XWPFDocument doc= new XWPFDocument();
        //记录需要合并的表格  根据行合并列
        JSONObject json=new JSONObject();//key 列ID VALYE JSON (begin end siz)

        CTDocument1 document = doc.getDocument();
        CTBody body = document.getBody();
        if(!body.isSetSectPr()){
            body.addNewSectPr();
        }
        CTSectPr section = body.getSectPr();

        if(!section.isSetPgSz()){
            section.addNewPgSz();
        }
        CTPageSz pageSize = section.getPgSz();
        pageSize.setW(BigInteger.valueOf(W));
        pageSize.setH(BigInteger.valueOf(H));
        pageSize.setOrient(STPageOrientation.LANDSCAPE);

        //添加标题
        XWPFParagraph titleParagraph = doc.createParagraph();

        //设置段落居中
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);

        XWPFRun titleParagraphRun = titleParagraph.createRun();
        titleParagraphRun.setText(title);
        titleParagraphRun.setColor("000000");
        //titleParagraphRun.isBold();
        titleParagraphRun.setFontSize(25);
        titleParagraphRun.addBreak();
        //添加 建表日期
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        XWPFParagraph titleParagraphTime = doc.createParagraph();
        titleParagraphTime.setAlignment(ParagraphAlignment.RIGHT);
        XWPFRun titleFunTime=titleParagraphTime.createRun();
        titleFunTime.setText("制单日期"+sim.format(new Date()));
        titleFunTime.addBreak();//换行

        //表格
        XWPFTable ComTable = doc.createTable();

        //设置指定宽度
        CTTbl ttbl = ComTable.getCTTbl();
        CTTblGrid tblGrid = ttbl.addNewTblGrid();
        for (int i : colWidths) {
            CTTblGridCol gridCol = tblGrid.addNewGridCol();
            gridCol.setW(new BigInteger(i+""));
        }

        //表头
        XWPFTableRow rowHead = ComTable.getRow(0);
        XWPFParagraph cellParagraph = (XWPFParagraph)rowHead.getCell(0).getParagraphs().get(0);
        cellParagraph.setAlignment(ParagraphAlignment.CENTER); //设置表头单元格居中
        XWPFRun cellParagraphRun  = cellParagraph.createRun();
        cellParagraphRun.setFontSize(10); //设置表头单元格字号
        cellParagraphRun.setBold(true); //设置表头单元格加粗
        cellParagraphRun.setText(value_columns[0]);
        for (int i = 1; i < value_columns.length; i++) {
            if(value_columns[i].indexOf("增减")>-1) {
                cellParagraph = (XWPFParagraph)rowHead.addNewTableCell().getParagraphs().get(0);
                cellParagraph.setAlignment(ParagraphAlignment.CENTER); //设置表头单元格居中
                cellParagraphRun  = cellParagraph.createRun();
                cellParagraphRun.setFontSize(10); //设置表头单元格居中
                cellParagraphRun.setBold(true);
                cellParagraphRun.setText("增减(+ / -)");
            }else {
                cellParagraph = (XWPFParagraph)rowHead.addNewTableCell().getParagraphs().get(0);
                cellParagraph.setAlignment(ParagraphAlignment.CENTER); //设置表头单元格居中
                cellParagraphRun  = cellParagraph.createRun();
                cellParagraphRun.setFontSize(10); //设置表头单元格居中
                cellParagraphRun.setBold(true); //设置表头单元格加粗
                cellParagraphRun.setText(value_columns[i]);
            }
        }
        int rows = list.size();
        //表格内容
        for (int i = 0; i < rows; i++) {
            XWPFTableRow rowsContent = ComTable.createRow();
            for (int j = 0; j < list.get(i).size(); j++) {
                XWPFParagraph cellParagraphC = (XWPFParagraph)rowsContent.getCell(j).getParagraphs().get(0);
                if(j!=one&&j!=two) {
                    cellParagraphC.setAlignment(ParagraphAlignment.CENTER); //设置表格内容居中
                }
                XWPFRun cellParagraphRunC  = cellParagraphC.createRun();
                cellParagraphRunC.setFontSize(10); //设置表格内容字号
                cellParagraphRunC.setText(list.get(i).get(j)+""); //单元格段落加载内容
            }
        }
        if(rows==0) {
            for (int i = 0; i < value_columns.length; i++) {
                XWPFTableCell cell = ComTable.getRow(0).getCell(i);
                cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); //垂直居中
            }
        }else {
            //设置居中
            for (int i = 0; i <= rows; i++) {
                for (int j = 0; j < list.get(0).size(); j++) {
                    XWPFTableCell cell = ComTable.getRow(i).getCell(j);
                    cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); //垂直居中
                }
            }
        }
        //合并单元格
        //mergeCellsVertically(ComTable,1,1,2);

        try {
            saveDocument(doc,"D:/expWordTest15.docx");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return doc;
    }

    /**
     *
     * @Date 2018年7月19日 上午11:19:21
     * @Description 判断是否是整数
     * @Fcunction isNumeric
     * @param str
     * @return boolean
     *
     */
    public static boolean isNumeric(String str) {
        if(str.indexOf(".")>0) {
            return false;
        }
        return true;
    }

    /**
     *
     * @Date 2018年7月19日 上午11:44:48
     * @Description 正式计划草案获取前两行
     * @Fcunction getCzzscaList
     * @param jnjhzh
     * @param trimpro
     * @param temp
     * @return List<List<String>>
     *
     */
    public static List<List<String>> getCzzscaList(String jnjhzh,String trimpro,String temp) {
        List<List<String>> list = new ArrayList<List<String>>();
        List<String> l1 = new ArrayList<String>(); //固定第一行
        l1.add("");
        l1.add("年度总计");
        l1.add("");
        l1.add("");
        l1.add("");
        l1.add("");
        l1.add(jnjhzh);
        l1.add("");
        l1.add("");
        l1.add("");
        list.add(l1);
        List<String> l2 = new ArrayList<String>(); //固定第二行
        l2.add("");
        l2.add("搜索总计");
        l2.add("");
        l2.add("");
        l2.add("");
        l2.add("");
        l2.add("");
//	    l2.add(trimpro+"\r100%");
        l2.add("");
        l2.add("");
        l2.add("");
        list.add(l2);
        return list;
    }
    public static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
        for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
            XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
            if (rowIndex == fromRow) {
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
            } else {
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
            }
        }

    }

    public static void saveDocument(XWPFDocument document, String savePath) throws IOException {
        OutputStream os = new FileOutputStream(savePath);
        document.write(os);
        os.close();
    }
    /*
        黄合并单元格 空格不合并
     */
    public void hb(List<List<String>> data){
            if(data==null||data.size()<=0){
                return;
            }
            int newRowfrom=0;
            int to=0;
            for(int i1=0;i1<data.size();i1++){
                if(i1==0){
                    continue;
                }
                List<String> listRow=data.get(i1);
                //判断是否是最后一行
                if(i1==(listRow.size()-1)){//最后一行暂时不处理

                }else{
                    if(listRow.get(0).equals(data.get(i1+1).get(0))){//判断是否相等
                        newRowfrom=i1;
                        continue;
                    }else{//继续遍历
                        if(newRowfrom!=to){

                        }else{
                            newRowfrom=i1;
                            to=i1;
                        }


                    }
                }
            }
    }
    /*
        根根行区间 合并列
     */


    }
